package service

import (
	"errors"

	"server/global"
	// "server/model/shop"
	"server/plugin/amall/model"
	"server/plugin/amall/model/request"
	// request "server/model/shop/request"
)

type CartService struct {
}

// CreateCart 创建购物车记录
// Author [piexlmax](https://github.com/piexlmax)
func (cartService *CartService) CreateCart(cart *model.Cart) (err error) {
	err = global.GVA_DB.Create(cart).Error
	return err
}

// DeleteCart 删除购物车记录
// Author [piexlmax](https://github.com/piexlmax)
func (cartService *CartService) DeleteCart(ID string) (err error) {
	err = global.GVA_DB.Delete(&model.Cart{}, "id = ?", ID).Error
	return err
}

// DeleteCartByIds 批量删除购物车记录
// Author [piexlmax](https://github.com/piexlmax)
func (cartService *CartService) DeleteCartByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]model.Cart{}, "id in ? ", IDs).Error
	return err
}

// UpdateCart 更新购物车记录
// Author [piexlmax](https://github.com/piexlmax)
func (cartService *CartService) UpdateCart(cart model.Cart) (err error) {
	err = global.GVA_DB.Model(&model.Cart{}).Where("id = ?", cart.ID).Updates(&cart).Error
	return err
}

// GetCart 根据ID获取购物车记录
// Author [piexlmax](https://github.com/piexlmax)
func (cartService *CartService) GetCart(ID string) (cart model.Cart, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&cart).Error
	return
}

// GetCartInfoList 分页获取购物车记录
// Author [piexlmax](https://github.com/piexlmax)
func (cartService *CartService) GetCartInfoList(info request.CartSearch) (list []model.Cart, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.Cart{})
	var carts []model.Cart
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}

	if info.UserID != 0 {
		db = db.Where("user_id = ?", info.UserID)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&carts).Error
	return carts, total, err
}

func (cartService *CartService) GetSelfCart(userID uint) (list []model.Cart, err error) {
	err = global.GVA_DB.Preload("Good").Preload("SKU").Find(&list, "user_id = ?", userID).Error
	return
}

func (cartService *CartService) AddCart(cartReq *request.CartCreate) (err error) {
	var cart model.Cart
	ferr := global.GVA_DB.First(&cart, "user_id = ? AND good_id = ? AND sku_id = ?", cartReq.UserID, cartReq.GoodID, cartReq.SKUID).Error
	if ferr != nil {
		cart.Quantity = 1
		cart.UserID = cartReq.UserID
		cart.GoodID = cartReq.GoodID
		cart.SKUID = cartReq.SKUID
		err = global.GVA_DB.Create(&cart).Error
		return
	}
	quantity := cart.Quantity + 1
	err = global.GVA_DB.Model(&cart).Update("quantity", quantity).Error
	return
}

func (cartService *CartService) CutCart(cartReq *request.CartCreate) (err error) {
	var cart model.Cart
	ferr := global.GVA_DB.First(&cart, "user_id = ? AND good_id = ? AND sku_id = ?", cartReq.UserID, cartReq.GoodID, cartReq.SKUID).Error
	if ferr != nil {
		return errors.New("购物车中没有该商品")
	}
	quantity := cart.Quantity - 1
	if quantity == 0 {
		err = global.GVA_DB.Delete(&cart).Error
		return
	}
	err = global.GVA_DB.Model(&cart).Update("quantity", quantity).Error
	return

}

func (cartService *CartService) ClearCart(userID uint) (err error) {
	err = global.GVA_DB.Delete(&model.Cart{}, "user_id = ?", userID).Error
	return
}

func (cartService *CartService) OrderByCart(ids []uint) (list []model.Cart, err error) {
	// TODO:从购物车下单
	return
}
